package dataStruct.queue.ex3;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Queue;
import java.util.concurrent.ExecutorService;

/**
 * Created by thinkpad on 2016/9/16.
 */
public class Producer implements Runnable{
    private Queue<MsgBody> msgQueue;
    private MsgTable msgTable;
    private ExecutorService service;
    private String msgName;

    public Producer(String msgName,Queue<MsgBody> msgQueue, MsgTable msgTable, ExecutorService service,  MsgBody msgBody, HashMap<Long, ThreadMsgInfo> threadMsgInfoHashMap) {
        this.msgQueue = msgQueue;
        this.msgTable = msgTable;
        this.service = service;
        this.msgName = msgName;
        this.threadMsgInfoHashMap = threadMsgInfoHashMap;
        this.msgBody = msgBody;
    }

    private HashMap<Long, ThreadMsgInfo> threadMsgInfoHashMap;

    public Producer(String msgName,Queue<MsgBody> msgQueue, MsgTable msgTable, ExecutorService service,  MsgBody msgBody) {
        this.msgQueue = msgQueue;
        this.msgTable = msgTable;
        this.service = service;
        this.msgName = msgName;
        this.msgBody = msgBody;
    }

    private MsgBody msgBody;
    public Producer(String msgName, Queue<MsgBody> msgQueue, MsgTable msgTable, ExecutorService service ) {
        this.msgName = msgName;
        this.msgQueue = msgQueue;
        this.msgTable = msgTable;
        this.service = service;
    }

//    @Override
//     public void run() {
//        while (true) {
//            Boolean insertFlag = true;
//            if (msgTable != null && msgQueue != null && msgTable.getMsgMap().size() > 0 && msgQueue.size() > 0) {
//                for (ThreadMsgInfo threadMsgInfo :threadMsgInfoHashMap.values()) {
//                    if (threadMsgInfo.getMsgBody().getName().equals(msgName) && threadMsgInfo.getState().equals(State.BUSY)) {
//                        insertFlag = false;
//                    }
//                }
//                if (insertFlag&&msgQueue.size()!=0) {
//                    System.out.println("Producer -----" + Thread.currentThread());
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    service.submit(new Scheduler(msgQueue, msgTable));
//                }
//            }
//        }
//    }

//    @Override
//    public void run() {
////        while (true) {
//            Boolean insertFlag = true;
//            if (msgTable != null && msgQueue != null) {
//                for (MsgBody value : msgTable.getMsgMap().values()) {
//                    if (value.getName().equals(msgName) && value.getState().equals(State.BUSY)) {
//                        insertFlag = false;
//                    }
//                }
//                if (insertFlag) {
//                    msgQueue.add(msgBody);
//                    msgTable.add(msgBody);
//                }
//            }
////        }
//    }

    @Override
    public void run() {
        while (true) {
            Boolean insertFlag = true;
            if (msgTable != null && msgQueue != null) {
                for (ThreadMsgInfo threadMsgInfo :threadMsgInfoHashMap.values()) {
                    if (threadMsgInfo.getMsgBody()!=null &&threadMsgInfo.getMsgBody().getName().equals(msgName) && threadMsgInfo.getState().equals(State.BUSY)
                            ||(msgTable.getMsgMap()!=null&&msgTable.getMsgMap().size()>0
                            &&!msgTable.getMsgMap().get(msgBody.getUuid()).getState().equals(State.DONE))) {
                        insertFlag = false;
                    }
                }
                if (insertFlag) {
                    msgQueue.offer(msgBody);
                    msgTable.add(msgBody);
//                    Calendar c = Calendar.getInstance();
                    System.out.println("Producer -----"+msgBody.getName() + "-----" + msgBody.getBody()+" " + Thread.currentThread());
                    break;
                }
            }
        }
    }
}
